home *** CD-ROM | disk | FTP | other *** search
/ ftp.qualcomm.com / 2014.06.ftp.qualcomm.com.tar / ftp.qualcomm.com / eudora / eudorapro / mac / extras / scripting / c-samples.hqx / AE to Eudora.c next >
Text File  |  1994-11-30  |  14KB  |  397 lines

  1. /*************************************************************************************
  2.  * AppleEvents to Eudora                                                             *
  3.  * by Scott Gruby (sgruby@qualcomm.com)                                              *
  4.  *                                                                                   *
  5.  * ⌐1994 QUALCOMM, Inc.                                                              *
  6.  *                                                                                   *
  7.  * Portions by Dave Perman, Shana Corp.                                              *
  8.  *************************************************************************************/
  9.  
  10.  
  11. #include <Aliases.h>
  12. #include "Eudora AE.h"
  13. #include <AERegistry.h>
  14. #include <AEObjects.h>
  15. #include <AppleEvents.h>
  16. #include <Processes.h>
  17.  
  18. #define attachDouble 0
  19. #define attachSingle 1
  20. #define attachBinHex 2
  21. #define attachUUencode 3
  22.  
  23. #define EU_Norm_Priority 0
  24. #define EU_High_Priority 60
  25. #define EU_Highest_Priority 1
  26. #define EU_Low_Priority 160
  27. #define EU_Lowest_Priority 200
  28.  
  29.  
  30.  
  31. void  MakeNullDesc (AEDesc *theDesc);
  32. OSErr MakeStringDesc (Str255 theStr,AEDesc *theDesc);
  33. OSErr Eud_CreateObjSpecifier (AEKeyword theClass,AEDesc theContainer,AEKeyword theForm,AEDesc theData,
  34.                               Boolean disposeInputs,AEDesc *theSpec);
  35. OSErr MakeCurrentMsgSpec (AEDesc *theSpec);
  36. OSErr Eud_SendEvent (AppleEvent *theEvent);
  37. OSErr Eud_MakeEvent (AEEventClass eventClass,AEEventID eventID,AppleEvent *theEvent);
  38. OSErr SendSetData(Str31 theFieldName,Ptr theData,long size);
  39. OSErr Eud_SendMessage(void);
  40. OSErr Set_Eudora_Priority(long thePriority);
  41.  
  42.  
  43.  
  44.  
  45. /*************************************************************************************
  46.  * MakeNullDesc (AEDesc *theDesc)                                                    *
  47.  *                                                                                   *
  48.  * This makes a Null AppleEvent descriptor.                                          *
  49.  *************************************************************************************/
  50.  
  51. void MakeNullDesc (AEDesc *theDesc)
  52. {
  53.     theDesc->descriptorType=typeNull;
  54.     theDesc->dataHandle=nil;
  55. }
  56.  
  57. /*************************************************************************************
  58.  * MakeStringDesc (Str255 theStr,AEDesc *theDesc)                                    *
  59.  *                                                                                   *
  60.  * This makes a string AppleEvent descriptor.                                        *
  61.  *************************************************************************************/
  62.  
  63. OSErr MakeStringDesc (Str255 theStr,AEDesc *theDesc)
  64. {
  65.     AECreateDesc(kAETextSuite,&theStr[1],StrLength(theStr),theDesc);
  66. }
  67.  
  68. /*************************************************************************************
  69.  * Eud_CreateObjSpecifier (AEKeyword theClass,AEDesc theContainer,                   *
  70.  *                         AEKeyword theForm,AEDesc theData,Boolean disposeInputs,   *
  71.  *                         AEDesc *theSpec)                                          *
  72.  *                                                                                   *
  73.  * This stuffs the required parameters into the AppleEvent.                          *
  74.  *************************************************************************************/
  75.  
  76. OSErr Eud_CreateObjSpecifier (AEKeyword theClass,AEDesc theContainer,AEKeyword theForm,AEDesc theData,
  77.                             Boolean disposeInputs,AEDesc *theSpec)
  78. {
  79.     AEDesc theRec;
  80.     OSErr    theErr;
  81.     
  82.     theErr=AECreateList(nil,0,true,&theRec);
  83.     if (!theErr)
  84.         theErr=AEPutKeyPtr(&theRec,keyAEKeyForm,typeEnumeration,&theForm,sizeof(theForm));
  85.     if (!theErr)
  86.         theErr=AEPutKeyPtr(&theRec,keyAEDesiredClass,cType,&theClass,sizeof(theClass));
  87.     if (!theErr)
  88.         theErr=AEPutKeyDesc(&theRec,keyAEKeyData,&theData);
  89.     if (!theErr)
  90.         theErr=AEPutKeyDesc(&theRec,keyAEContainer,&theContainer);
  91.     if (!theErr)
  92.         theErr=AECoerceDesc(&theRec,cObjectSpecifier,theSpec);
  93.     AEDisposeDesc(&theRec);
  94.     return theErr;
  95. }
  96.  
  97. /*************************************************************************************
  98.  * MakeCurrentMsgSpec (AEDesc *theSpec)                                              *
  99.  *                                                                                   *
  100.  * This creates an AEDesc for the current message.                                   *
  101.  *************************************************************************************/
  102.  
  103. OSErr MakeCurrentMsgSpec (AEDesc *theSpec)
  104. {
  105.     AEDesc theContainer,theIndex;
  106.     OSErr    theErr;
  107.     long    msgIndex=1;
  108.  
  109.     MakeNullDesc (&theContainer);
  110.     theErr=AECreateDesc(cLongInteger,&msgIndex,sizeof(msgIndex),&theIndex);
  111.     if (!theErr)
  112.         theErr=Eud_CreateObjSpecifier(cEuMessage,theContainer,formAbsolutePosition,theIndex,true,theSpec);
  113.     AEDisposeDesc(&theContainer);
  114.     AEDisposeDesc(&theIndex);
  115.     return theErr;
  116. }
  117.  
  118.  
  119. /*************************************************************************************
  120.  * Eud_SendEvent (AppleEvent *theEvent)                                              *
  121.  *                                                                                   *
  122.  * This sends a given AppleEvent to Eudora                                           *
  123.  *************************************************************************************/
  124.  
  125. OSErr Eud_SendEvent (AppleEvent *theEvent)
  126. {
  127.     AppleEvent theReply;
  128.     OSErr err,err1;
  129.     long theTime;
  130.     
  131.     MakeNullDesc(&theReply);
  132.     err=AESend(theEvent,&theReply,kAENoReply + kAENeverInteract + kAECanSwitchLayer+kAEDontRecord,kAENormalPriority,-1,nil,nil);
  133.  
  134.     AEDisposeDesc(&theReply);
  135.     AEDisposeDesc(theEvent);
  136.  
  137.     return err;
  138. }
  139.  
  140.  
  141. /*************************************************************************************
  142.  * Eud_MakeEvent (AEEventClass eventClass,AEEventID eventID,AppleEvent *theEvent)    *
  143.  *                                                                                   *
  144.  * This creates an AppleEvent that will be eventually sent to Eudora.                *
  145.  * The event class and ID must be specified when calling this                        *
  146.  *************************************************************************************/
  147.  
  148. OSErr Eud_MakeEvent (AEEventClass eventClass,AEEventID eventID,AppleEvent *theEvent)
  149. {
  150.     AEAddressDesc theTarget;
  151.     OSType theSignature;
  152.     OSErr    theErr;
  153.     
  154.     theSignature=kEudoraSuite;
  155.     theErr=AECreateDesc(typeApplSignature,&theSignature,sizeof(theSignature),&theTarget);
  156.     if (!theErr)
  157.         theErr=AECreateAppleEvent(eventClass,eventID,&theTarget,0,0,theEvent);
  158.     AEDisposeDesc(&theTarget);
  159.     return theErr;
  160. }
  161.  
  162.  
  163. /*************************************************************************************
  164.  * SendSetData(Str31 theFieldName,Ptr thePtr,long size)                              *
  165.  *                                                                                   *
  166.  * This sets the data in a specified field. It operates on the frontmost message     *
  167.  * in Eudora. It is the equivalent of sending the following AppleScript:             *
  168.  * set field "fieldname" of message 0 to "data"                                      *
  169.  *************************************************************************************/
  170.  
  171. OSErr SendSetData(Str31 theFieldName,Ptr thePtr)
  172. {
  173.     AEDesc theMsg,theName,theFieldSpec,theText;
  174.     AppleEvent theEvent;
  175.     OSErr theErr;
  176.     Handle theData;
  177.     long    size=StrLength(thePtr);
  178.     
  179.     theData=NewHandle(size);
  180.     BlockMove((Ptr)thePtr+1,*theData,size);
  181.  
  182.     if (theData!=nil)
  183.         {
  184.  
  185.             theErr=MakeCurrentMsgSpec(&theMsg);
  186.             if (!theErr)
  187.                 theErr=MakeStringDesc(theFieldName,&theName);
  188.             if (!theErr)
  189.                 theErr=Eud_CreateObjSpecifier(cEuField,theMsg,formName,theName,true,&theFieldSpec);
  190.             if (!theErr)
  191.                 theErr=Eud_MakeEvent(kAECoreSuite,kAESetData,&theEvent);
  192.             if (!theErr)
  193.                 theErr=AEPutParamDesc(&theEvent,keyAEResult,&theFieldSpec);
  194.             AEDisposeDesc(&theFieldSpec);
  195.                 
  196.             theText.descriptorType=typeChar;
  197.             theText.dataHandle=theData;
  198.             if (!theErr)
  199.                 theErr=AEPutParamDesc(&theEvent,keyAEData,&theText);
  200.             if (!theErr)
  201.                 theErr=Eud_SendEvent(&theEvent);
  202.  
  203.             DisposHandle(theText.dataHandle);
  204.             AEDisposeDesc(&theText);
  205.             AEDisposeDesc(&theMsg);
  206.             AEDisposeDesc(&theName);
  207.         }
  208.     DisposHandle(theData);
  209.     return theErr;
  210. }
  211.  
  212. /*************************************************************************************
  213.  * Eud_SendMessage(void)                                                             *
  214.  *                                                                                   *
  215.  * This is an example of how the AppleEvents in Eudora can be used.                  *
  216.  *************************************************************************************/
  217.  
  218. OSErr Eud_SendMessage(void)
  219. {    
  220.     AEDesc nullSpec,theName,theFolder,theMailbox,theInsertRec,theInsl,msgSpec,theEnclList;
  221.     OSType thePos,theClass;
  222.     AppleEvent theEvent;
  223.     Boolean trueValue;
  224.     OSErr     theErr;
  225.     OSType    myType;
  226.     long    thePriority;
  227.     long    count;
  228. /* These are sample variables for the different fields. You are not limited to 255 characters;
  229.    this example is using Str255s to simplify things.
  230. */
  231.     Str255    toRecipient="\peudora-sales@qualcomm.com",
  232.             ccRecipient="\peudora-sales@qualcomm.com",
  233.             bccRecipient="\peudora-sales@qualcomm.com",
  234.             subject="\pTest Message",
  235.             body="\pThis is the message body.";
  236.     FSSpec    theEnclosure;
  237.     
  238.         
  239. /* This section creates a new message and places it at the end of the out mailbox.
  240.    It is equivalent to  the following AppleScript:
  241.         make message at end of mailbox "out" of mail folder ""
  242. */
  243.  
  244.  
  245.     MakeNullDesc(&nullSpec);
  246.     theErr=MakeStringDesc("\p",&theName);
  247.     if (!theErr)
  248.         theErr=Eud_CreateObjSpecifier(cEuMailfolder,nullSpec,formName,theName,true,&theFolder);
  249.  
  250.  
  251.     if (!theErr)
  252.         theErr=MakeStringDesc("\pout",&theName);
  253.     if (!theErr)
  254.         theErr=Eud_CreateObjSpecifier(cEuMailbox,theFolder,formName,theName,true,&theMailbox);
  255.     if (!theErr)
  256.         theErr=AECreateList(nil,0,true,&theInsertRec);
  257.     if (!theErr)
  258.         theErr=AEPutKeyDesc(&theInsertRec,keyAEObject,&theMailbox);
  259.  
  260.  
  261.     thePos=kAEEnd;
  262.     if (!theErr)
  263.         theErr=AEPutKeyPtr(&theInsertRec,keyAEPosition,typeEnumeration,&thePos,sizeof(thePos));
  264.  
  265.     if (!theErr)
  266.         theErr=AECoerceDesc(&theInsertRec,typeInsertionLoc,&theInsl);
  267.  
  268.     if (!theErr)
  269.         theErr=Eud_MakeEvent(kAECoreSuite,kAECreateElement,&theEvent);
  270.     if (!theErr)
  271.         theErr=AEPutParamDesc(&theEvent,keyAEInsertHere,&theInsl);
  272.  
  273.     AEDisposeDesc(&nullSpec);
  274.     AEDisposeDesc(&theName);
  275.     AEDisposeDesc(&theFolder);
  276.     AEDisposeDesc(&theMailbox);
  277.     AEDisposeDesc(&theInsertRec);
  278.     AEDisposeDesc(&theInsl);
  279.  
  280.     theClass=cEuMessage;
  281.     if (!theErr)
  282.         theErr=AEPutParamPtr(&theEvent,keyAEObjectClass,cType,&theClass,sizeof(theClass));
  283.     if (!theErr)
  284.         theErr=Eud_SendEvent(&theEvent);
  285.  
  286.  
  287. /* This section fills in various fields.
  288.    It is equivalent to  the following AppleScript:
  289.         set field "to" of message 0 to "data"
  290. */
  291.  
  292.     if (!theErr)
  293.         theErr=SendSetData("\pto",toRecipient);
  294.     
  295.     if (!theErr)
  296.         theErr=SendSetData("\pcc",ccRecipient);
  297.     if (!theErr)
  298.         theErr=SendSetData("\pbcc",bccRecipient,);
  299.  
  300.     if (!theErr)
  301.         theErr=SendSetData("\psubject",subject,);
  302.     if (!theErr)
  303.         theErr=SendSetData("\p",body,);
  304.  
  305. /* This sets the priority of the message. See the constants defined above for the legal
  306.    values.
  307. */
  308.     thePriority=EU_Low_Priority;
  309.     if (!theErr)
  310.         theErr=Set_Eudora_Priority(thePriority);
  311.         
  312.  
  313. /* This attaches a file to the Eudora message provided it is a proper FSSpec. */
  314.     if (StrLength(theEnclosure.name)>0)
  315.         {
  316.             if (!theErr)
  317.                 theErr=MakeCurrentMsgSpec(&msgSpec);
  318.             if (!theErr)
  319.                 theErr=Eud_MakeEvent(kEudoraSuite,kEuAttach,&theEvent);
  320.             if (!theErr)
  321.                 theErr=AEPutParamDesc(&theEvent,keyAEResult,&msgSpec);
  322.                 if (!theErr)
  323.                     theErr=AECreateList(nil,0,false,&theEnclList);
  324.                 if (!theErr)
  325.                     theErr=AEPutPtr(&theEnclList,0,typeFSS,&theEnclosure.name,sizeof(theEnclosure.name);
  326.                 if (!theErr)
  327.                     theErr=AEPutParamDesc(&theEvent,keyEuDocumentList,&theEnclList);
  328.                 if (!theErr)
  329.                     theErr=Eud_SendEvent(&theEvent);
  330.                 AEDisposeDesc(&msgSpec);
  331.                 AEDisposeDesc(&theEnclList);
  332.             }
  333.  
  334.  
  335. /* This tells Eudora to queue the current message. */
  336.     if (!theErr)
  337.         theErr=MakeCurrentMsgSpec(&msgSpec);
  338.     if (!theErr)
  339.         theErr=Eud_MakeEvent(kEudoraSuite,kEuQueue,&theEvent);
  340.     if (!theErr)
  341.         theErr=AEPutParamDesc(&theEvent,keyAEResult,&msgSpec);
  342.     AEDisposeDesc(&msgSpec);
  343.     if (!theErr)
  344.         theErr=Eud_SendEvent(&theEvent);
  345.  
  346. }
  347.  
  348.  
  349. /*************************************************************************************
  350.  * Set_Eudora_Priority(long thePriority)                                             *
  351.  *                                                                                   *
  352.  * Given the priority of a message, this sets the priority in the message.           *
  353.  * This same type of procedure can be used for many of the AppleScript commands in   *
  354.  * the form of:                                                                      *
  355.  * set <item> of message 0 to <data>                                                 *
  356.  *************************************************************************************/
  357.  
  358. OSErr Set_Eudora_Priority(long thePriority)
  359. {
  360.   AEDesc theMsg,theData,thePropSpec,thePriorityDesc;
  361.   AppleEvent theEvent,theReply;
  362.   OSErr theErr;
  363.   AEKeyword theProperty;
  364.   Handle h;
  365.  
  366.   theErr = MakeCurrentMsgSpec(&theMsg);
  367.  
  368.   theProperty = pEuPriority;
  369.   AECreateDesc(typeType,&theProperty,sizeof (theProperty),&thePriorityDesc);
  370.  
  371.   if (!theErr)
  372.      theErr=Eud_CreateObjSpecifier(typeProperty,theMsg,typeProperty,thePriorityDesc,true,&thePropSpec);
  373.   if (!theErr)
  374.     theErr=Eud_MakeEvent(kAECoreSuite,kAESetData,&theEvent);
  375.  
  376.   if (!theErr)
  377.     theErr=AEPutKeyDesc(&theEvent, keyDirectObject, &thePropSpec);
  378.   if (!theErr)
  379.     theErr=AEDisposeDesc(&thePropSpec);
  380.  
  381.   h = NewHandle (sizeof(thePriority));
  382.   BlockMove ((Ptr)&thePriority,*h,sizeof(thePriority));
  383.   theData.descriptorType = typeInteger;
  384.   theData.dataHandle = h;
  385.   if (!theErr)
  386.     theErr=AEPutParamDesc(&theEvent,keyAEData,&theData);
  387.   if (!theErr)
  388.     theErr=Eud_SendEvent(&theEvent);
  389.     DisposeHandle(h);
  390.     AEDisposeDesc(&theMsg);
  391.     AEDisposeDesc(&theData);
  392.     AEDisposeDesc(&thePriorityDesc);
  393.  
  394.    return theErr;
  395.  
  396. }
  397.